package org.idansof.otp.ui;

import java.util.List;

import org.idansof.otp.client.*;
import org.mapsforge.android.maps.MapActivity;
import org.mapsforge.android.maps.MapView;
import org.mapsforge.android.maps.MapViewMode;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DisplayPlanActivity extends MapActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		final PlanResult result = (PlanResult) getIntent().getExtras().get("plan");
		
		setContentView(R.layout.plan);
		ListView listView = (ListView) findViewById(R.id.planExpandableList);
		listView.setAdapter(new BaseAdapter() {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				
				
		        if (convertView == null) {
		            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		            convertView = vi.inflate(R.layout.plan_itinerary, null);
		        }

		       String timeFormat="kk:mm";
		        
				Itinerary itinerary = result.getTripPlan().getItineraries().get(position);
				TextView text = (TextView) convertView.findViewById(R.id.plan_itinerary_text);
				TextView leaveAtClock = (TextView) convertView.findViewById(R.id.leave_at_clock);
				TextView arriveAtClock = (TextView) convertView.findViewById(R.id.arrive_at_clock);
				leaveAtClock.setText(DateFormat.format(timeFormat, itinerary.getStartTime()));
				arriveAtClock.setText(DateFormat.format(timeFormat, itinerary.getEndTime()));
				
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.append(getText(R.string.itinerary_option));
				stringBuilder.append(" ");
				stringBuilder.append(position+1);
				stringBuilder.append(" : ");
				stringBuilder.append((int) itinerary.getDuration()/1000/60);
				stringBuilder.append(" ");
				stringBuilder.append(getText(R.string.itinerary_minutes));
				stringBuilder.append(" ");
				stringBuilder.append(itinerary.getTransfers());
				stringBuilder.append(" ");
				stringBuilder.append(getText(R.string.itinerary_transfers));
				text.setText(stringBuilder.toString());
				
				return convertView;
				
			}
			
			@Override
			public long getItemId(int position) {
				return result.getTripPlan().getItineraries().get(position).hashCode();
			}
			
			@Override
			public Object getItem(int position) {
				return result.getTripPlan().getItineraries().get(position);
			}
			
			@Override
			public int getCount() {
				return result.getTripPlan().getItineraries().size();
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Intent itineraryIntent = new Intent(getApplicationContext(),DisplayItineraryActivity.class);
				itineraryIntent.putExtra("itinerary", result.getTripPlan().getItineraries().get(position));
				itineraryIntent.putExtra("location",result.getTripPlan().getFrom());
				startActivity(itineraryIntent);
				
			}
			
		});
		
	}

}
